#include "ObjectList.hpp"
#include <algorithm>

ObjectList::ObjectList(std::shared_ptr<Object> obj) {
    add(obj);
}

ObjectList::~ObjectList() {
}

void ObjectList::add(std::shared_ptr<Object> obj) {
    objs.push_back(obj);
}

std::optional<ShadePayload> ObjectList::hit(const Ray& ray)const {
    std::optional<ShadePayload> payload;
    float nearest = std::numeric_limits<float>::infinity();
    for (const auto& obj : objs) {
        auto p = obj->hit(ray);
        if (p && p->tNear<nearest) {
            nearest = p->tNear;
            payload = std::move(p);
        }
    }

    return payload;
}